package org.neogenesis.web.dao;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;

import org.neogenesis.web.Configurations;
import org.neogenesis.web.Utils;

import org.neogenesis.web.model.Experiment;
import org.neogenesis.web.model.Platform;
import org.neogenesis.web.model.Sample;
import org.neogenesis.web.model.SampleType;
import org.neogenesis.web.qualifier.DataRepository;

@SessionScoped
public class ExperimentDAO implements Serializable {

	@Inject
	@DataRepository
	private EntityManager em;

	private String uploadedFilePath;

	private String fileGSEPath;

	private String folderGSEPath;

	private SampleDAO sampleDAO;

	private PlatformDAO platformDAO;

	public ExperimentDAO() {
		// TODO Auto-generated constructor stub
	}

	public void create(Experiment experiment) {
		em.getTransaction().begin();

		em.persist(experiment);
		em.getTransaction().commit();
	}

	public void mergeListSample(Experiment experiment, List<Sample> samples) {

		experiment.setSamples(samples);
		em.getTransaction().begin();

		em.merge(experiment);
		em.getTransaction().commit();
	}

	public Experiment findByName(String name) {

		Query q = em.createQuery(
				"SELECT e FROM Experiment e WHERE e.seriesIid = :name",
				Experiment.class);
		q.setParameter("name", name);

		List<Experiment> list = q.getResultList();

		if (list.size() > 0) {
			return list.get(0);
		}

		return null;
	}

	public List<Experiment> findAll() {
		CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
		cq.select(cq.from(Experiment.class));
		return em.createQuery(cq).getResultList();
	}

	public List<Experiment> findRange(int[] range) {
		CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
		cq.select(cq.from(Experiment.class));
		javax.persistence.Query q = em.createQuery(cq);
		q.setMaxResults(range[1] - range[0]);
		q.setFirstResult(range[0]);
		return q.getResultList();
	}

	public int count() {
		javax.persistence.criteria.CriteriaQuery cq = em.getCriteriaBuilder()
				.createQuery();
		javax.persistence.criteria.Root<Experiment> rt = cq
				.from(Experiment.class);
		cq.select(em.getCriteriaBuilder().count(rt));
		javax.persistence.Query q = em.createQuery(cq);
		return ((Long) q.getSingleResult()).intValue();
	}

	public List<Sample> sampleOfExperiment(Experiment exp) {
		Query q = em
				.createQuery("select s.name from Sample s, Experiment e where e.id=s.experiment.id AND experiment.seriesIid=:exp");
		q.setParameter("exp", exp.getSeriesIid());

		List<Sample> s = q.getResultList();
		return s;

	}

	public void remove(Experiment experiment) {
		em.getTransaction().begin();
		em.remove(em.merge(experiment));
		em.getTransaction().commit();
	}

}
